var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var common = require("common");
var nf = sm("do_Notification");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var urlPub = require("url/pub");
var imgTool = require("helper/picture");
var root = ui("$");
var httpUrl = urlPub.url.report.doAdd;
var bizzId = null; //业务类型对应ID
var bizzType = null; //业务类型
var reasonId = null; //举报原因

//引入公共heander
var headerAout = ui("headerReport");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "举报",
    "labelNext": "发送"
});
header.refreshData();

var content = ui("content");
var btnSubmit = ui("btnSubmit");
var linearLayout_main = ui("do_LinearLayout_main");

/****************点击-添加图片-begin*****************/
var imgGridView = ui("imgGrid");
var imgMax = 3;  //最多显示图片
var imgsData = []; //保存图片数组
/*添加图片*/
var do_ALayout_camera = ui("do_ALayout_camera");
do_ALayout_camera.on("touch", function(data, e) {
	if (imgMax==0) {
		nf.toast("最多只能上传 3 张图片");
		return;
	}
	imgTool.imgMulti(imgMax, selectImgBack);// 选择多张图片
});
/*删除图片*/
imgGridView.on("touch",function(data, e){
	imgMax=imgMax+1;
	imgsData.splice(data,1);
	imgGridView.items = imgsData;
});
function selectImgBack(imgList){
	imgMax=imgMax-imgList.length;
	imgsData = imgsData.concat(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.items = imgsData;
}
/****************点击-添加图片-end*****************/

//andriod返回键
page.on("back", function() {
    app.closePage();
});

//获取上一个页面传入的参数
page.on("loaded", function(){
	var params = page.getData();
	bizzId = params.bizzId;
	bizzType = params.bizzType;
});

//提交
btnSubmit.on("touch","",5000, function(){
	submit();
});

function submit(){
	page.hideKeyboard();
	if(reasonId === null){
		nf.toast("请选择举报原因!");
		btnSubmit.enabled = true;
		return false;
	}
	
	var imgCount = imgsData.length;//图片数量
	if(content.text == ""){
		nf.toast("请输入举报内容!");
		btnSubmit.enabled = true;
		return false;
	}
	
	var imgFiles  = [];
	for (var i = 0; i < imgCount; i++) {
		imgFiles.push(imgsData[i].imgPath);
	}
	if(imgCount>0){
		http.form(imgFiles,"pub/report",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			var addParams = {
		            "bizzId":bizzId,
		            "bizzType":bizzType,
					"content":content.text,
					"reason":reasonId,
					"img":string4imgs
				};
			httpSilence.post(httpUrl,addParams,function(data){
				nf.toast("举报成功");
				app.closePage();
			});
		});	
	}else{
		var addParams = {
	            "bizzId":bizzId,
	            "bizzType":bizzType,
				"content":content.text,
				"reason":reasonId,
				"img":""
			};
		http.post(httpUrl,addParams,function(data){
			nf.toast("举报成功");
			app.closePage();
		});
	}

	btnSubmit.enabled = false;
}


var addReason = linearLayout_main.add("reason", "source://view/reuse/form/mutiLabel.ui",0,0);
var reason = ui("reason");
reason.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "举报原因",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
reason.refreshData();
var reasonLabelText = ui(addReason+".labelText");
var addPicker = root.add("picker","source://view/reuse/form/oneCategory.ui",0,0);
var picker = ui("picker");
//举报原因
reason.on("touch","",300,function(){
	page.hideKeyboard();
	var param={}
	var ptitle={"title":"选择举报原因","id":"reasonId"}
	var datas = [{id:"1",name:"色情低俗"},{id:"2",name:"欺诈骗钱"},{id:"3",name:"广告骚扰"}];
	var pcount = [ptitle,datas];
	page.fire("oneCategoryList",pcount);
	page.fire("oneCategory","true");
});
page.on("refreshOneCategory",function(data){
	if("reasonId" == data[1]){
		reasonId = data[0].oneid;  //
		reasonLabelText.text = data[0].onename;
	}
});

//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})